//
//  GDTJCChooseContentView.m
//  PockPower
//
//  Created by EDZ on 2017/7/11.
//  Copyright © 2017年 XingHang. All rights reserved.
//

#import "GDTJCChooseContentView.h"
#import "LineView.h"
#import "ConditionModel.h"

@interface GDTJCChooseContentView () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UIView * backView;
@property (nonatomic, strong)LineView * lineLabel;
@property (nonatomic, strong)UIView * superBackView;

@end
#define kBackViewHeight 270

@implementation GDTJCChooseContentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - life cycle

- (void)dealloc {
    NSLog(@"%@", NSStringFromClass([self class]));
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self dataInit];
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSelfMethod)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    self.backView = [[UIView alloc] initWithFrame:(CGRectMake(0, CGRectGetMaxY(self.frame), kMainScreenWidth, kBackViewHeight))];
    _backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_backView];
    
    self.titleLab = [UILabel new];
    _titleLab.font = [UIFont systemFontOfSize:16];
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.text = self.title ? self.title : @"";
    _titleLab.backgroundColor = [UIColor whiteColor];
    [_backView addSubview:_titleLab];
    
    self.cancelBtn = [self makeCommonBtnWithTitle:@"取消"color:[UIColor grayColor]];
    [_backView addSubview:_cancelBtn];
    
    self.doneBtn = [self makeCommonBtnWithTitle:@"完成" color:[UIColor orangeColor]];
    [_backView addSubview:_doneBtn];

    self.tableView = [[UITableView alloc] init];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_backView addSubview:_tableView];

    self.lineLabel = [LineView new];
    [_backView addSubview:_lineLabel];


}

- (void)layoutInit {
    weakself(ws);

    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws.backView);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(ws.backView.mas_width).multipliedBy(0.5);
        make.top.mas_equalTo(ws.backView.mas_top);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.backView).offset(kMargin15);
        make.centerY.mas_equalTo(ws.titleLab);
        make.height.mas_equalTo(ws.titleLab);
        make.width.mas_equalTo(50);
    }];

    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kMargin15);
        make.centerY.mas_equalTo(ws.cancelBtn);
        make.height.mas_equalTo(ws.cancelBtn);
        make.width.mas_equalTo(ws.cancelBtn);
    }];
    
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.backView);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(ws.tableView.mas_top);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(ws.backView);
        make.top.mas_equalTo(ws.titleLab.mas_bottom);//.multipliedBy(0.4);
    }];

    [UIView animateWithDuration:0.3 animations:^{
        ws.backView.frame = CGRectMake(0, ws.xj_height - kBackViewHeight - 64,  ws.xj_width, kBackViewHeight);
    }];
}

- (void)dataInit {
    
//    self.dataArray = @[@"1", @"2", @"3", @"4", @"5", @"1", @"2", @"3", @"4", @"5"];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLab.text = self.title ? self.title : @"";
}


- (UIButton *)makeCommonBtnWithTitle:(NSString *)title color:(UIColor *)color{
    UIButton * btn = [UIButton new];
    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    return btn;
}

- (void)dismissSelfMethod {
    weakself(weakself);
    [UIView animateWithDuration:0.3 animations:^{
    
        weakself.backView.frame = CGRectMake(0, CGRectGetMaxY(weakself.frame), weakself.xj_width, kBackViewHeight);
    } completion:^(BOOL finished) {
        
        [weakself.backView removeFromSuperview];
        [weakself removeFromSuperview];
        
    }];

}

- (void)doneMethod {
    ConditionModel * conditionModel = nil;
    for (int i = 0; i < _dataArray.count; i++) {
        ConditionModel * tconditionModel = _dataArray[i];
        if (tconditionModel.isSelected) {
            conditionModel = tconditionModel;
        }
    }
    
    if (conditionModel) {
        self.clickBlock ? self.clickBlock(conditionModel) : @"";
    }

    [self dismissSelfMethod];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:self.tableView]) {
        return NO;
    }
    
    return YES;
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return  _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"
                                                             forIndexPath:indexPath];
    cell.textLabel.font = kGeneralFontSize;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ConditionModel * conditionModel = _dataArray[indexPath.row];
    cell.textLabel.text = conditionModel.value;
    if (conditionModel.isSelected) {
        cell.textLabel.textColor = [UIColor orangeColor];
    }else {
        cell.textLabel.textColor = [UIColor grayColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (int i = 0; i < _dataArray.count; i++) {
        ConditionModel * conditionModel = _dataArray[i];
        conditionModel.isSelected = NO;
    }
    
    ConditionModel * conditionModel = _dataArray[indexPath.row];
    conditionModel.isSelected = YES;
    [tableView reloadData];
}


@end
